package rps.game;

import java.rmi.RemoteException;
import java.util.Collection;

import rps.game.data.Figure;
import rps.game.data.FigureKind;
import rps.game.data.Move;
import rps.game.data.Player;

public class ValidatingGame implements Game {

	private final Game game;
	private final Player player;

	public ValidatingGame(Game game, Player player) throws RemoteException {
		this.player = player;
		this.game = game;
	}

	@Override
	public void setInitialAssignment(Player p, FigureKind[] assignment) throws RemoteException {
		if(assignment.length == 42 && checkAssignment(assignment) && this.player.getNick().equals(p.getNick()))
		game.setInitialAssignment(p, assignment);
		else {
			throw new IllegalArgumentException();
		}
	}
	
	private boolean checkAssignment(FigureKind[] array){
		int nRock     = 0;
		int nPaper    = 0;
		int nScissor  = 0;
		int nFlag	  = 0;
		int nTrap	  = 0;
		int nFree     = 0;
		for (int j =28 ; j<42;j++){		
			if (array[j] == null) {
				nFree++;
			} else if(array[j].equals(FigureKind.SCISSORS)) {
				nScissor ++;
			} else if(array[j].equals(FigureKind.ROCK)) {
				nRock ++;
			} else if(array[j].equals(FigureKind.PAPER)) {
				nPaper ++;
			} else if(array[j].equals(FigureKind.FLAG)) {
				nFlag ++;
			} else if(array[j].equals(FigureKind.TRAP)) {
				nTrap ++;
			}
		}
		if(nRock == 4 && nPaper == 4 && nScissor == 4 && nFlag == 1 && nTrap ==1)
			return true;
		else
			return false;
	}

	@Override
	public Figure[] getField() throws RemoteException {
		return game.getField();
	}

	
	private boolean isMoveIn (Collection<Move> posMoves, Move m) {
		for (Move move : posMoves) {
			if (move.getFrom() == m.getFrom() && move.getTo() == m.getTo()) {
				return true;
			}
		}
		
		return false;
	}
	@Override
	public void move(Player p, int from, int to) throws RemoteException {
		Movement var = new Movement(game, p);
		if(!this.player.getNick().equalsIgnoreCase(p.getNick()))
			throw new IllegalArgumentException("wrong player");
		else if(!(isMoveIn(var.getPosMoves(), new Move(from, to, game.getField())))) {
			throw new IllegalArgumentException("no legal move");
		} else
			game.move(p, from, to);
	}

	@Override
	public Move getLastMove() throws RemoteException {
		return game.getLastMove();
	}

	@Override
	public void sendMessage(Player p, String message) throws RemoteException {
		game.sendMessage(p, message);
	}

	@Override
	public void setInitialChoice(Player p, FigureKind kind) throws RemoteException {
		if(!(kind.equals(FigureKind.TRAP) || kind.equals(FigureKind.HIDDEN) || kind.equals(FigureKind.FLAG)))
		game.setInitialChoice(p, kind);
		else
			throw new IllegalArgumentException();
	}

	@Override
	public void setUpdatedKindAfterDraw(Player p, FigureKind kind) throws RemoteException {
		if(!(kind.equals(FigureKind.TRAP) || kind.equals(FigureKind.HIDDEN) || kind.equals(FigureKind.FLAG)))
		game.setUpdatedKindAfterDraw(p, kind);
		else
			throw new IllegalArgumentException();
	}

	@Override
	public void surrender(Player p) throws RemoteException {
		game.surrender(p);
	}

	@Override
	public Player getOpponent(Player p) throws RemoteException {
		return game.getOpponent(p);
	}
}